﻿using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour {

	GameObject cubo;
	public GameObject contador;
	public GameObject cuboBase;
	public GameObject cuboAzul;
	public GameObject cuboVerde;
	public GameObject cuboVermelho;
	public GameObject cuboAmarelo;
	public GameObject cuboBranco;
	public GameObject cuboPreto;
	int cuboCor;
	int cubosForma;
	int cubosFormaAnterior;
	public Texture bTexture;
	public Texture setaDir;
	public Texture setaEsq;
	public GUIStyle meuStyle;
	public static bool cubosCaindo;
	public static bool calcular;
	public static float tempo;
	public static float[] tempoLinhas;
	public static float[] arrayPosCubosY;
	public static float[] arrayPosCubosX;
	public static int[] arrayLinhas;
	public static int[] arrayColunas;
	public static int[] arrayBlocosLinhas;
	public static int[] arrayBlocosColunas;

	public static int descerReferencia;
	public static int descerReferenciaAnt;
	public static int cubosDescer = 0;

	public static int movimentosDir = 0;
	public static int movimentosEsq = 0;
	public static int cubosCont = 0;
	public static int moveCont = 0;
	public static int cubosDescendo = 0;

	public static int[] cuboAzulcont;
	public static int[] cuboVerdecont;
	public static int[] cuboVermelhocont;
	public static int[] cuboAmarelocont;

	public static bool descida = false;
	public static bool[] verificar;
	public static bool[] linhaVazia;
	public static int[] contarCubos;
	public static int linhaReferencia;

	// Use this for initialization
	void Start ()
	{
		arrayPosCubosY = new float[14];
		arrayPosCubosX = new float[6];

		descerReferencia = 20;
		descerReferenciaAnt = 20;
		linhaVazia = new bool[14];
		verificar = new bool[14];
		cuboAzulcont = new int[14];
		cuboVerdecont = new int[14];
		cuboVermelhocont = new int[14];
		cuboAmarelocont = new int[14];
		tempoLinhas = new float[14];
		contarCubos = new int[14];
		for (int i = 0; i < 14; i++)
		{
			linhaVazia[i] = false;
			verificar[i] = false;
			cuboAzulcont[i] = 0;
			cuboVerdecont[i] = 0;
			cuboVermelhocont[i] = 0;
			cuboAmarelocont[i] = 0;
			tempoLinhas[i] = 0;
			contarCubos[i] = 0;
		}

		cubosCaindo = false;
		cubo = cuboBase;

//		for (int i = 0; i < 12; i++)
//		{
//			arrayPosCubosY[i] = (this.transform.position.y + cubo.renderer.bounds.size.y/2) + i*0.68f;
//		}
//		for (int i = 0; i < 5; i++)
//		{
//			arrayPosCubosX[i] = (this.transform.position.x + cubo.renderer.bounds.size.x/2) + i*0.68f;
//		}

		for (int i = 0; i < 14; i++)
		{
			//arrayPosCubosY[i] = (this.transform.position.y + 0.30f) + i*0.62f;
			arrayPosCubosY[i] = (this.transform.position.y + 0.30f) + i*0.70f;
		}
		for (int i = 0; i < 6; i++)
		{
			//arrayPosCubosX[i] = (this.transform.position.x + 0.20f) + i*0.62f;
			arrayPosCubosX[i] = (this.transform.position.x + 0.20f) + i*0.70f;
		}

		for (int i = 0; i < 12; i++)
		{
			Instantiate(contador,
			            //new Vector3(arrayPosCubosX[0] + 1.55f,
			            new Vector3(arrayPosCubosX[0] + 1.74f,
			            arrayPosCubosY[i],
			            this.transform.position.z),
			            this.transform.rotation);

			for (int j = 0; j < 6; j++)
			{
				Instantiate(cubo,
				            new Vector3(arrayPosCubosX[j],
				            			arrayPosCubosY[i],
				            			this.transform.position.z),
				            this.transform.rotation);
			}
		}

		arrayLinhas = new int[14];
		arrayColunas = new int[6];
		for (int i = 0; i < 14; i++)
		{
			arrayLinhas[i] = i + 1;
		}
		for (int i = 0; i < 6; i++)
		{
			arrayColunas[i] = i;
		}

//		Instantiate(cubo,
//		            new Vector3((this.transform.position.x + cubo.renderer.bounds.size.x/2) + 0*0.68f,
//		            			(this.transform.position.y + cubo.renderer.bounds.size.y/2) + 0*0.68f,
//		            			 this.transform.position.z),
//		            this.transform.rotation);
//		Instantiate(cubo,
//		            new Vector3((this.transform.position.x + cubo.renderer.bounds.size.x/2) + 1*0.68f,
//		            			(this.transform.position.y + cubo.renderer.bounds.size.y/2) + 0*0.68f,
//		            			 this.transform.position.z),
//		            this.transform.rotation);
//		Instantiate(cubo,
//		            new Vector3((this.transform.position.x + cubo.renderer.bounds.size.x/2) + 0*0.68f,
//		            			(this.transform.position.y + cubo.renderer.bounds.size.y/2) + 1*0.68f,
//		            			 this.transform.position.z),
//		            this.transform.rotation);
	}

	// Update is called once per frame
	void Update ()
	{
		tempo += 1*Time.deltaTime;
		if (tempo > 1)
		{
			tempo = 0;
		}

		if (cubosCaindo == false && tempo == 0)
		{
			CriarForma();
		}

		if (descerReferencia != descerReferenciaAnt)
		{
			descerReferenciaAnt = descerReferencia;
		}
	}
	
	void CriarForma()
	{
		// Sorteia o numero do cubo
		cubosForma = Random.Range(0,2);
		if (cubosFormaAnterior != cubosForma)
		{
			cubosFormaAnterior = cubosForma;
		}else{
			cubosForma += 1;
			if (cubosForma > 1)
			{
				cubosForma = 0;
			}
			cubosFormaAnterior = cubosForma;
		}

		//Quadrado
		if (cubosForma == 0)
		{
			movimentosDir = 2;
			movimentosEsq = 2;

			TrocaCubos();
			Instantiate(cubo,
			            new Vector3(arrayPosCubosX[2],
			            arrayPosCubosY[0] + 9,
			            this.transform.position.z),
			            this.transform.rotation);
			TrocaCubos();
			Instantiate(cubo,
			            new Vector3(arrayPosCubosX[3],
			            arrayPosCubosY[0] + 9,
			            this.transform.position.z),
			            this.transform.rotation);
			TrocaCubos();
			Instantiate(cubo,
			            new Vector3(arrayPosCubosX[2],
			            (arrayPosCubosY[0] + 9) + (cubo.renderer.bounds.size.y + 0.10f),
			            this.transform.position.z),
			            this.transform.rotation);
			TrocaCubos();
			Instantiate(cubo,
			            new Vector3(arrayPosCubosX[3],
			            (arrayPosCubosY[0] + 9) + (cubo.renderer.bounds.size.y + 0.10f),
			            this.transform.position.z),
			            this.transform.rotation);

			cubosCont = 4;
			moveCont = 4;
		}

		//Reta Deitada
		if (cubosForma == 1)
		{
			movimentosDir = 1;
			movimentosEsq = 1;

			TrocaCubos();
			Instantiate(cubo,
			            new Vector3(arrayPosCubosX[1],
			            arrayPosCubosY[0] + 9,
			            this.transform.position.z),
			            this.transform.rotation);
			TrocaCubos();
			Instantiate(cubo,
			            new Vector3(arrayPosCubosX[2],
			            arrayPosCubosY[0] + 9,
			            this.transform.position.z),
			            this.transform.rotation);
			TrocaCubos();
			Instantiate(cubo,
			            new Vector3(arrayPosCubosX[3],
			            arrayPosCubosY[0] + 9,
			            this.transform.position.z),
			            this.transform.rotation);
			TrocaCubos();
			Instantiate(cubo,
			            new Vector3(arrayPosCubosX[4],
			            arrayPosCubosY[0] + 9,
			            this.transform.position.z),
			            this.transform.rotation);

			cubosCont = 4;
			moveCont = 4;
		}
		
		cubosCaindo = true;
	}
	
//	void OnGUI()
//	{
//		// Fecha o Jogo
//		if (GUI.Button(new Rect(10,10,70,50), bTexture, meuStyle))
//		{
//			Application.Quit();
//		}
//
//		// Move Blocos
//		if (GUI.Button(new Rect(80,650,20,10), setaDir, meuStyle))
//		{
//			movDireita = true;
//		}
//		if (GUI.Button(new Rect(20,650,20,10), setaEsq, meuStyle))
//		{
//			movEsquerda = true;
//		}
//	}

	// Troca o Prefab do Cubo a ser chamado
	void TrocaCubos()
	{
		cuboCor = 0;//Random.Range(0,4);

		if (cuboCor == 0)
		{
			cubo = cuboAzul;
		}
		if (cuboCor == 1)
		{
			cubo = cuboVerde;
		}
		if (cuboCor == 2)
		{
			cubo = cuboVermelho;
		}
		if (cuboCor == 3)
		{
			cubo = cuboAmarelo;
		}
	}

	public static void contarLinha(int linha)
	{
		contarCubos[linha] = 0;
		contarCubos[linha] += cuboAzulcont[linha];
		contarCubos[linha] += cuboVerdecont[linha];
		contarCubos[linha] += cuboVermelhocont[linha];
		contarCubos[linha] += cuboAmarelocont[linha];

		//Debug.Log ("Primeira Linha com " + contarCubos[1]);

		if (contarCubos[linha] == 0)
		{
			linhaVazia[linha] = true;
		}else{
			linhaVazia[linha] = false;
		}
	}
}
